package com.cqan.message.controller;

import static com.cqan.common.ViewName.MESSAGE_LIST;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cqan.account.entity.User;
import com.cqan.controller.BaseController;
import com.cqan.message.entity.Message;
import com.cqan.message.service.MessageService;
import com.cqan.orm.PropertyFilter;

/**
 * 消息控制器
 * @author slaton
 *
 */
@Controller
@RequestMapping("/message")
public class MessageController extends BaseController<Message> {
	
	@Resource
	private MessageService messageService;
	
	@RequestMapping("/{box}/list.htm")
	public ModelAndView list(Integer pageNo,HttpServletRequest request,@PathVariable  String box){
		
		ModelAndView mav = new ModelAndView(MESSAGE_LIST);
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		if (box==null||"".equals(box)) {
			box = "inbox";
		}
		if (box.equalsIgnoreCase("inbox")) {
			User user = getUserDetails();
			if (user!=null) {
				page.setPageNo(pageNo);
				page.setOrder("desc");
				page.setOrderBy("ctime");
				page = messageService.getInboxMessage(page,user);
				mav.addObject("page", page);
			}
			
		}else if (box.equalsIgnoreCase("outbox")) {
			User user = getUserDetails();
			if (user!=null) {
				filters.add(new PropertyFilter("EQL_fromid",String.valueOf(user.getId())));
				page.setPageNo(pageNo);
				page.setOrder("desc");
				page.setOrderBy("ctime");
				page = messageService.search(page, filters);
				mav.addObject("page", page);
			}
		}
		return mav;
	}

}
